package com.boot.aiproject.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.boot.aiproject.service.AiService;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class AiServiceImpl implements AiService {
    @Override
    public String chat(String question) {

        String url="https://gy.raojailong6.asia/v1/chat/completions";
        String json="{\n" +
                "    \"model\": \"gpt-3.5-turbo-16k\",\n" +
                "    \"messages\": [\n" +
                "     \n" +
                "      {\n" +
                "        \"role\": \"user\",\n" +
                "        \"content\": \""+question+"\"\n" +
                "      }\n" +
                "    ] }";


        //创建一个请求体对象(body)
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody requestBody= RequestBody.create(mediaType, json);

        Request request = new Request.Builder().post(requestBody)
                .addHeader("content-Type","application/ison")
                .addHeader("Authorization", "Bearer sk-tEDujo8DPiqY7AH458298eA9CdA84cE3Ac3b3fA1E31aB095")
                .url(url).build();

        OkHttpClient okHttpclient = new OkHttpClient().newBuilder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60,TimeUnit.SECONDS).
        readTimeout(60,TimeUnit.SECONDS).
        build();
        String content="";
        try{
            Response response = okHttpclient.newCall(request).execute();
            String retStr = response.body().string();
           JSONObject jsonObject = JSON.parseObject(retStr);
            JSONArray choices = jsonObject.getJSONArray("choices");
            if(null!=choices && choices.size()>0){
                JSONObject jsonObject1 = (JSONObject) choices.get(0);
                JSONObject message = jsonObject1.getJSONObject("message");
                content = message.getString("content");

            }


            System.out.println(content);
        }catch (Exception e){
            content="返回错误，解析失败";
            log.error(content+":{}",e.getMessage());
//            e.printStackTrace();
        }

      return content;
    }

    @Override
    public String img(String question) {


        String url="https://gy.raojailong6.asia/v1/images/generations";

        String json="{\n" +
                "    \"prompt\": \""+question+"\",\n" +
                "    \"n\":1,\n" +
                "    \"size\": \"1024x1024\"\n" +
                "}";


        //创建一个请求体对象(body)
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody requestBody= RequestBody.create(mediaType, json);

        Request request = new Request.Builder().post(requestBody)
                .addHeader("content-Type","application/ison")
                .addHeader("Authorization", "Bearer sk-tEDujo8DPiqY7AH458298eA9CdA84cE3Ac3b3fA1E31aB095")
                .url(url).build();

        OkHttpClient okHttpclient = new OkHttpClient().newBuilder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60,TimeUnit.SECONDS).
                readTimeout(60,TimeUnit.SECONDS).
                build();
        String content="";
        try{
            Response response = okHttpclient.newCall(request).execute();
            String retStr = response.body().string();
            JSONObject jsonObject = JSON.parseObject(retStr);
            JSONArray choices = jsonObject.getJSONArray("data");
            if(null!=choices && choices.size()>0){
                Object o = choices.get(0);
                JSONObject jsonObject1 = JSON.parseObject(o.toString());
                content = jsonObject1.getString("url");

            }


            System.out.println(content);
        }catch (Exception e){
            content="返回错误，解析失败";
            log.error(content+":{}",e.getMessage());
        }

        return content;
    }


}
